﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using AjaxControlFramework.Reflection;


[assembly: WebResource("AjaxControlFramework.CreateTemplate.debug.js", "application/x-javascript")]
[assembly: WebResource("AjaxControlFramework.CreateTemplate.release.js", "application/x-javascript")]

[assembly: WebResource("AjaxControlFramework.DynamicCreateTemplate.debug.js", "application/x-javascript")]
[assembly: WebResource("AjaxControlFramework.DynamicCreateTemplate.release.js", "application/x-javascript")]

[assembly: WebResource("AjaxControlFramework.ControlTemplate.debug.js", "application/x-javascript")]
[assembly: WebResource("AjaxControlFramework.ControlTemplate.release.js", "application/x-javascript")]

[assembly: WebResource("AjaxControlFramework.NamedMethodTemplate.debug.js", "application/x-javascript")]
[assembly: WebResource("AjaxControlFramework.NamedMethodTemplate.release.js", "application/x-javascript")]

[assembly: WebResource("AjaxControlFramework.NamedPropertyTemplate.debug.js", "application/x-javascript")]
[assembly: WebResource("AjaxControlFramework.NamedPropertyTemplate.release.js", "application/x-javascript")]

[assembly: WebResource("AjaxControlFramework.NamedControlTemplate.debug.js", "application/x-javascript")]
[assembly: WebResource("AjaxControlFramework.NamedControlTemplate.release.js", "application/x-javascript")]


namespace AjaxControlFramework
{
	public class ControlScriptStrategy : ScriptStrategy
	{	
		//------// Properties \\--------------------------------------------\\
		public virtual string CreateTemplateName
		{
			get
			{
				if (base.BuildMode == ScriptBuildMode.Debug)
				{
                    return "AjaxControlFramework.CreateTemplate.debug.js";
				}
				else
				{
                    return "AjaxControlFramework.CreateTemplate.release.js";
				}
			}
		}


        public virtual string DynamicCreateTemplateName
        {
            get
            {
                if (base.BuildMode == ScriptBuildMode.Debug)
                {
                    return "AjaxControlFramework.DynamicCreateTemplate.debug.js";
                }
                else
                {
                    return "AjaxControlFramework.DynamicCreateTemplate.release.js";
                }
            }
        }


		public virtual string ControlTemplateName
		{
			get
			{
				if (base.BuildMode == ScriptBuildMode.Debug)
				{
                    return "AjaxControlFramework.ControlTemplate.debug.js";
				}
				else
				{
                    return "AjaxControlFramework.ControlTemplate.release.js";
				}
			}
		}


		public virtual string NamedMethodTemplateName
		{
			get
			{
				if (base.BuildMode == ScriptBuildMode.Debug)
				{
                    return "AjaxControlFramework.NamedMethodTemplate.debug.js";
				}
				else
				{
                    return "AjaxControlFramework.NamedMethodTemplate.release.js";
				}
			}
		}


        public virtual string NamedPropertyTemplateName
        {
            get
            {
                if (base.BuildMode == ScriptBuildMode.Debug)
                {
                    return "AjaxControlFramework.NamedPropertyTemplate.debug.js";
                }
                else
                {
                    return "AjaxControlFramework.NamedPropertyTemplate.release.js";
                }
            }
        }


        public virtual string NamedControlTemplateName
        {
            get
            {
                if (base.BuildMode == ScriptBuildMode.Debug)
                {
                    return "AjaxControlFramework.NamedControlTemplate.debug.js";
                }
                else
                {
                    return "AjaxControlFramework.NamedControlTemplate.release.js";
                }
            }
        }


        private string _typeShorthandName = null;
        public virtual string TypeShorthandName
        {
            get
            {
                if (_typeShorthandName == null)
                {
                    _typeShorthandName = "$ac";

                    if (HttpContext.Current.Items["AjaxControlFramework.ControlScriptStrategy.RenderedControlID:" + BaseTargetControlType.FullName] == null)
                    {
                        int shorthandID = 0;

                        if (HttpContext.Current.Items["AjaxControlFramework.ControlScriptStrategy.RenderedControlID"] == null)
                        {
                            shorthandID = 1;
                        }
                        else
                        {
                            shorthandID = (int)HttpContext.Current.Items["AjaxControlFramework.ControlScriptStrategy.RenderedControlID"] + 1;
                        }

                        HttpContext.Current.Items["AjaxControlFramework.ControlScriptStrategy.RenderedControlID"] = shorthandID;
                        HttpContext.Current.Items["AjaxControlFramework.ControlScriptStrategy.RenderedControlID:" + BaseTargetControlType.FullName] = shorthandID;
                        _typeShorthandName += shorthandID.ToString();
                    }
                    else
                    {
                        _typeShorthandName += (int)HttpContext.Current.Items["AjaxControlFramework.ControlScriptStrategy.RenderedControlID:" + BaseTargetControlType.FullName];
                    }
                }

                return _typeShorthandName;
            }
        }
		//------\\ Properties //--------------------------------------------//



		//------// Constructors \\------------------------------------------\\
		public ControlScriptStrategy(Control targetControl)
			: base(targetControl)
		{ }


		public ControlScriptStrategy(Control targetControl, ScriptBuildMode buildMode)
			: base(targetControl, buildMode)
		{ }
		//------\\ Constructors //------------------------------------------//



		//------// Methods \\-----------------------------------------------\\
		public override void GenerateCreateScript(ref TextWriter writer)
		{
			if (writer == null) { throw new ArgumentNullException("writer"); }

            writer.Write(ReplaceCreateTokens(GetCreateScript(), null));
		}


        protected virtual string ReplaceCreateTokens(string createScript, Dictionary<string, object> additionalProperties, params string[] tokenReplacements)
        {
            createScript = createScript.Replace("$escapedbasetargettype$", BaseTargetControlType.FullName.Replace('.', '_'));
            createScript = createScript.Replace("$shorthand$", TypeShorthandName);

            if (additionalProperties == null)
            {
                additionalProperties = new Dictionary<string,object>();
            }


            additionalProperties.Add("ID", TargetControl.ID);
            additionalProperties.Add("ClientID", TargetControl.ClientID);
            additionalProperties.Add("UniqueID", TargetControl.UniqueID);
            additionalProperties.Add("LoadOutOfBand", (Control.CallbackStrategy.LoadOutOfBand.HasValue && Control.CallbackStrategy.LoadOutOfBand.Value));
            additionalProperties.Add("ClientIDMode", (int)AjaxControlUtils.GetEffectiveClientIDMode(TargetControl));

            if (TargetControl.NamingContainer is IDataItemContainer)
            {
                additionalProperties.Add("HasDataContainer", true);
            }

            /*if (TargetControl.NamingContainer is DataBoundControl || TargetControl.NamingContainer is IDataItemContainer)
            {
                additionalProperties = new Dictionary<string, object>(1);

                if (Control.CallbackStrategy.LoadAutomatically.Value)
                {
                    if (TargetControl.NamingContainer is DataBoundControl)
                    {
                        additionalProperties.Add("AutoLoadID", AjaxControlUtils.GetUniqueIDPrefix(TargetControl.NamingContainer.NamingContainer) + TargetControl.ID);
                    }
                    else if (TargetControl.NamingContainer is IDataItemContainer)
                    {
                        additionalProperties.Add("AutoLoadID", AjaxControlUtils.GetUniqueIDPrefix(TargetControl.NamingContainer.NamingContainer.NamingContainer) + TargetControl.ID);
                    }
                    else
                    {
                        additionalProperties.Add("AutoLoadID", TargetControl.UniqueID);
                    }
                }
            }*/

            createScript = createScript.Replace("$properties$", GenerateInitialPropertyValuesScript(additionalProperties));
            createScript = createScript.Replace("$initialstate$", (Control.AjaxContext.State == null || Control.AjaxContext.State.Count == 0 ? "null" : Control.CallbackStrategy.Serializer.SerializeCallbackResult(Control.AjaxContext.State)));

            return createScript;
        }


        public override void GenerateDynamicCreateScript(ref TextWriter writer, bool invokeDynamicRegister)
        {
            if (writer == null) { throw new ArgumentNullException("writer"); }

            // TODO: The use of this "invokeDynamicRegister" parameter is really crappy and weak design. Fix this Lou.
            if (invokeDynamicRegister)
            {
                writer.Write(ReplaceDynamicCreateTokens(GetDynamicCreateScript(), null));
            }
            else
            {
                writer.Write(ReplaceDynamicCreateTokens(GetCreateScript(), null));
            }
        }


        protected virtual string ReplaceDynamicCreateTokens(string dynamicCreateScript, Dictionary<string, object> additionalProperties, params string[] tokenReplacements)
        {
            dynamicCreateScript = dynamicCreateScript.Replace("$escapedbasetargettype$", BaseTargetControlType.FullName.Replace('.', '_'));
            dynamicCreateScript = dynamicCreateScript.Replace("$shorthand$", BaseTargetControlType.FullName);

            if (additionalProperties == null)
            {
                additionalProperties = new Dictionary<string, object>();
            }

            additionalProperties.Add("ID", TargetControl.ID);
            additionalProperties.Add("ClientID", TargetControl.ClientID);
            additionalProperties.Add("UniqueID", TargetControl.UniqueID);
            additionalProperties.Add("LoadOutOfBand", true);
            additionalProperties.Add("ClientIDMode", (int)AjaxControlUtils.GetEffectiveClientIDMode(TargetControl));

            if (AjaxControlHandler.CallbackArguments.Properties.ContainsKey("HasDataContainer") && (bool)AjaxControlHandler.CallbackArguments.Properties["HasDataContainer"] == true)
            {
                additionalProperties.Add("HasDataContainer", true);
            }

            dynamicCreateScript = dynamicCreateScript.Replace("$properties$", GenerateInitialPropertyValuesScript(additionalProperties));
            dynamicCreateScript = dynamicCreateScript.Replace("$initialstate$", (Control.AjaxContext.State == null || Control.AjaxContext.State.Count == 0 ? "null" : Control.CallbackStrategy.Serializer.SerializeCallbackResult(Control.AjaxContext.State)));

            return dynamicCreateScript;
        }


        protected virtual string GenerateInitialPropertyValuesScript(Dictionary<string, object> additionalProperties)
        {
            Dictionary<string, PropertyGetCacheEntry> getEntries = PropertyCache.GetPropertyGettersOfType(TargetControlType);

            Dictionary<string, object> propertyValues = new Dictionary<string, object>();

            foreach (PropertyGetCacheEntry getEntry in getEntries.Values)
            {
                if (getEntry.Attribute.Protected && Authenticator != null && (!Authenticator.Authenticated || !Authenticator.UserIsAuthorized(getEntry.Attribute.AuthorizedRoles)))
                {
                    continue;
                }


                if (getEntry.Delegate != null && getEntry.Attribute != null)
                {
                    object propertyValue = getEntry.Delegate(TargetControl);
                    if (propertyValue == null && getEntry.Attribute.DefaultValue != null) { continue; }

                    propertyValues.Add(getEntry.ItemName, propertyValue);
                }
            }

            // Attach any additional properties that should be rendered.
            if (additionalProperties != null)
            {
                foreach (KeyValuePair<string, object> additionalProperty in additionalProperties)
                {
                    propertyValues.Add(additionalProperty.Key, additionalProperty.Value);
                }
            }

            return (propertyValues.Count == 0 ? "null" : Control.CallbackStrategy.Serializer.SerializeObject(propertyValues));
        }


		public override void GenerateControlScript(ref TextWriter writer)
		{
			if (writer == null) { throw new ArgumentNullException("writer"); }

            writer.Write(ReplaceControlTokens(GetControlScript()));

			GenerateNamedMethodsScript(ref writer);
            GenerateNamedPropertiesScript(ref writer);
            GenerateNamedControlsScript(ref writer);
		}


        protected virtual string ReplaceControlTokens(string controlScript, params string[] tokenReplacements)
        {
            controlScript = controlScript.Replace("$basetargettype$", BaseTargetControlType.FullName);
            controlScript = controlScript.Replace("$parenttype$", "AjaxControl");
            controlScript = controlScript.Replace("$shorthand$", TypeShorthandName);
            controlScript = controlScript.Replace("$qualifiedtargettype$", TargetControlType.AssemblyQualifiedName);

            return controlScript;
        }


		public override void GenerateNamedMethodsScript(ref TextWriter writer)
		{
            if (writer == null) { throw new ArgumentNullException("writer"); }

            Dictionary<string, MethodCacheEntry> methods = MethodCache.GetMethodsOfType(TargetControlType);

            if (methods.Count > 0)
            {
                writer.Write(ReplaceNamedMethodTokens(methods, GetNamedMethodScript()));
            }
		}


        protected virtual string ReplaceNamedMethodTokens(Dictionary<string, MethodCacheEntry> methods, string namedMethodScript, params string[] tokenReplacements)
        {
            bool authenticated = (Authenticator == null ? false : Authenticator.Authenticated);
            StringBuilder namedMethodBuilder = new StringBuilder();


            foreach (MethodCacheEntry method in methods.Values)
            {
                if (method.Attribute.Protected && Authenticator != null && (!Authenticator.Authenticated || !Authenticator.UserIsAuthorized(method.Attribute.AuthorizedRoles)))
                {
                    continue;
                }


                string methodScript = namedMethodScript;

                methodScript = methodScript.Replace("$shorthand$", TypeShorthandName);
                methodScript = methodScript.Replace("$methodname$", method.ItemName);

                StringBuilder parametersScript = new StringBuilder();

                GenerateParametersScript(method, ref parametersScript);

                methodScript = methodScript.Replace("$parametersarray$", (parametersScript.Length > 0 ? parametersScript.ToString() : String.Empty));
                methodScript = methodScript.Replace("$parameters$", (parametersScript.Length > 0 ? parametersScript.ToString() + ", " : String.Empty));
                methodScript = methodScript.Replace("$trackviewstate$", (!method.Attribute.TrackViewState.HasValue || method.Attribute.TrackViewState.Value ? "true" : "false"));

                namedMethodBuilder.Append(methodScript);
            }


            return namedMethodBuilder.ToString();
        }


        protected virtual void GenerateParametersScript(MethodCacheEntry method, ref StringBuilder parametersScript)
        {
            ParameterInfo[] parameters = method.Info.GetParameters();

            if (parameters.Length > 0)
            {
                for (int index = 0; index < parameters.Length - 1; index++)
                {
                    parametersScript.Append(parameters[index].Name).Append(", ");
                }

                parametersScript.Append(parameters[parameters.Length - 1].Name);
            }
        }


        public override void GenerateNamedPropertiesScript(ref TextWriter writer)
        {
            if (writer == null) { throw new ArgumentNullException("writer"); }

            Dictionary<string, PropertyGetCacheEntry> properties = PropertyCache.GetPropertyGettersOfType(TargetControlType);

            if (properties.Count > 0)
            {
                writer.Write(ReplaceNamedPropertyTokens(properties, GetNamedPropertyScript()));
            }
        }


        protected virtual string ReplaceNamedPropertyTokens(Dictionary<string, PropertyGetCacheEntry> properties, string namedPropertyScript, params string[] tokenReplacements)
        {
            bool authenticated = (Authenticator == null ? false : Authenticator.Authenticated);

            StringBuilder propertyScript = new StringBuilder();
            int propertyCount = 0;


            foreach (PropertyGetCacheEntry property in properties.Values)
            {
                // ['$propertyname$',$defaultvalue$],['$propertyname$',$defaultvalue$], ...

                if (property.Attribute.Protected && Authenticator != null && (!Authenticator.Authenticated || !Authenticator.UserIsAuthorized(property.Attribute.AuthorizedRoles)))
                {
                    continue;
                }


                GeneratePropertyScript(property, ref propertyScript);

                if (propertyCount < properties.Count - 1)
                {
                    propertyScript.Append(',');
                }
                
                propertyCount++;
            }

            namedPropertyScript = namedPropertyScript.Replace("$shorthand$", TypeShorthandName);
            namedPropertyScript = namedPropertyScript.Replace("$properties$", propertyScript.ToString());

            return namedPropertyScript;
        }


        protected virtual void GeneratePropertyScript(PropertyGetCacheEntry property, ref StringBuilder propertyScript)
        {
            // ['$propertyname$',$defaultvalue$],['$propertyname$',$defaultvalue$], ...

            propertyScript.Append("['").Append(property.ItemName).Append("'");

            if (property.Attribute.DefaultValue == null)
            {
                propertyScript.Append(",true]"); // Adding "true" parameter to enable Serialization for the property.
            }
            else
            {
                propertyScript.Append(",'").Append(Control.CallbackStrategy.Serializer.SerializeObject(property.Attribute.DefaultValue)).Append("',true]"); // Adding "true" parameter to enable Serialization for the property.
            }
        }


        public override void GenerateNamedControlsScript(ref TextWriter writer)
        {
            if (writer == null) { throw new ArgumentNullException("writer"); }

            AjaxControlControlsAttribute controlsAttribute = (AjaxControlControlsAttribute)TargetControlType.GetCustomAttributes(typeof(AjaxControlControlsAttribute), true).FirstOrDefault();
            if (controlsAttribute == null) { return; }

            List<string> controlIDs = new List<string>(controlsAttribute.ControlIDs);
            List<NamedControlDefinition> namedControls = new List<NamedControlDefinition>(controlIDs.Count);

            if (controlsAttribute.AutoDetect)
            {
                ICollection<Control> controls = AjaxControlUtils.FindNamedChildControls(TargetControl, !controlsAttribute.SearchChildren);

                foreach (Control control in controls)
                {
                    if (!controlIDs.Contains(control.ID))
                    {
                        // Provide extra search criteria (that is, the rest of the control's ClientID after that of the TargetControl). This allows for
                        // controls that are nested deep into Control hierarchy of the TargetControl to still be found on the client-side.
                        namedControls.Add(new NamedControlDefinition(control.ID, control.ClientID.Replace(TargetControl.ClientID + "_", String.Empty)));
                    }
                    else
                    {
                        // Remove the ID from the "controlIDs" collection so that it doesn't get added a second time when we loop through that collection to add 
                        // any remaining "declared" controls that were not actually found in the Control hierarchy.
                        controlIDs.Remove(control.ID);
                    }
                }


                // Add all those remaining control IDs that were "declared" by the AjaxControlControls attribute but weren't actually found in the TargetControl 
                // hierarchy. These need to be added even though they weren't found because they were declared by the developer and are expected to resolve 
                // and not be "undefined" when their JavaScript runs.
                foreach (string controlID in controlIDs)
                {
                    // Since the control in scope here wasn't actually found in the TargetControl's Control hierarchy, we can't provide the additional information 
                    // required for it to resolve on the client-side if it is nested below the top level of the hierarchy. Nonetheless, it will be declared on 
                    // the client-side, but just won't return a DOM element when any JavaScript tries to access it by name.
                    namedControls.Add(new NamedControlDefinition(controlID));
                }
            }
            else
            {
                bool targetControlIsPage = (TargetControl is Page);
                ICollection<Control> controls = AjaxControlUtils.FindControls(TargetControl, !controlsAttribute.SearchChildren, controlsAttribute.ControlIDs);

                foreach (string controlID in controlIDs)
                {
                    bool controlAdded = false;

                    foreach (Control control in controls)
                    {
                        //if (controlID.EndsWith(control.ID))
                        if (controlID == control.ID) // Case-sensitive.
                        {
                            if (control is IAjaxControl)
                            {
                                if (targetControlIsPage)
                                {
                                    namedControls.Add(new NamedControlDefinition(control.ID, control.ID, NamedControlSearchType.AjaxControl_Page));
                                }
                                else
                                {
                                    namedControls.Add(new NamedControlDefinition(control.ID, control.ID, NamedControlSearchType.AjaxControl));
                                }
                            }
                            else if (targetControlIsPage && control.NamingContainer != null)
                            {
                                if (control.NamingContainer is Page)
                                {
                                    namedControls.Add(new NamedControlDefinition(control.ID, control.ID, NamedControlSearchType.ID_Page));
                                }
                                else
                                {
                                    namedControls.Add(new NamedControlDefinition(control.ID, control.ClientID, NamedControlSearchType.ID_Page));
                                }
                            }
                            else
                            {
                                DataBoundControl dataBoundParent = AjaxControlUtils.FindParentControlByType<DataBoundControl>(control, TargetControl);

                                // Provide extra search criteria (that is, the rest of the control's ClientID after that of the TargetControl). This allows for
                                // controls that are nested deep into Control hierarchy of the TargetControl to still be found on the client-side.
                                if (dataBoundParent != null)
                                {
                                    namedControls.Add(new NamedControlDefinition(control.ID, control.ClientID.Replace(TargetControl.ClientID + "_", String.Empty)));
                                }
                                else
                                {
                                    namedControls.Add(new NamedControlDefinition(control.ID, control.ID));
                                }
                            }

                            controlAdded = true;

                            break;
                        }
                    }

                    // Add all those remaining control IDs that were "declared" by the AjaxControlControls attribute but weren't actually found in the TargetControl 
                    // hierarchy. These need to be added even though they weren't found because they were declared by the developer and are expected to resolve 
                    // and not be "undefined" when their JavaScript runs.
                    if (!controlAdded)
                    {
                        // Since the control in scope here wasn't actually found in the TargetControl's Control hierarchy, we can't provide the additional information 
                        // required for it to resolve on the client-side if it is nested below the top level of the hierarchy. Nonetheless, it will be declared on 
                        // the client-side, but just won't return a DOM element when any JavaScript tries to access it by name.
                        namedControls.Add(new NamedControlDefinition(controlID));
                    }
                }
            }


            if (controlIDs.Count > 0)
            {
                writer.Write(ReplaceNamedControlTokens(namedControls, GetNamedControlScript()));
            }
        }


        /*public override void GenerateNamedControlsScript(ref TextWriter writer)
        {
            if (writer == null) { throw new ArgumentNullException("writer"); }
            
            AjaxControlControlsAttribute controlsAttribute = (AjaxControlControlsAttribute)TargetControlType.GetCustomAttributes(typeof(AjaxControlControlsAttribute), true).FirstOrDefault();
            if (controlsAttribute == null) { return; }

            ICollection<Control> controls = null;

            if (controlsAttribute.AutoDetect)
            {
                controls = AjaxControlUtils.FindNamedChildControls(TargetControl, !controlsAttribute.SearchChildren);
            }
            else if (controlsAttribute.ControlIDs != null && controlsAttribute.ControlIDs.Length > 0)
            {
                controls = AjaxControlUtils.FindControls(TargetControl, !controlsAttribute.SearchChildren, controlsAttribute.ControlIDs);
            }


            if (controls != null && controls.Count > 0)
            {
                writer.Write(ReplaceNamedControlTokens(controls, GetNamedControlScript()));
            }
        }*/


        protected virtual string ReplaceNamedControlTokens(List<NamedControlDefinition> namedControls, string namedControlScript, params string[] tokenReplacements)
        {
            StringBuilder controlScript = new StringBuilder();
            int controlCount = 0;

            foreach (NamedControlDefinition namedControl in namedControls)
            {
                // [['$controlname$'],['$controlname$'],...]
                if (!String.IsNullOrWhiteSpace(namedControl.ControlName) && !String.IsNullOrWhiteSpace(namedControl.SearchCriteria) && (namedControl.ControlName != namedControl.SearchCriteria || namedControl.SearchType == NamedControlSearchType.AjaxControl || namedControl.SearchType == NamedControlSearchType.AjaxControl_Page))
                {
                    controlScript.Append("['").Append(namedControl.ControlName).Append("','").Append(namedControl.SearchCriteria).Append("',").Append(((int)namedControl.SearchType).ToString()).Append("]");
                }
                else if (!String.IsNullOrWhiteSpace(namedControl.ControlName))
                {
                    controlScript.Append("['").Append(namedControl.ControlName).Append("']");
                }

                if (controlCount < namedControls.Count - 1)
                {
                    controlScript.Append(',');
                }

                controlCount++;
            }

            namedControlScript = namedControlScript.Replace("$shorthand$", TypeShorthandName);
            namedControlScript = namedControlScript.Replace("$controls$", controlScript.ToString());

            return namedControlScript;
        }


        /*protected virtual string ReplaceNamedControlTokens(ICollection<Control> controls, string namedControlScript, params string[] tokenReplacements)
        {
            StringBuilder controlScript = new StringBuilder();
            int controlCount = 0;

            foreach (Control control in controls)
            {
                // [['$controlname$'],['$controlname$'],...]

                controlScript.Append("['").Append(control.ID).Append("']");

                if (controlCount < controls.Count - 1)
                {
                    controlScript.Append(',');
                }

                controlCount++;
            }

            namedControlScript = namedControlScript.Replace("$shorthand$", TypeShorthandName);
            namedControlScript = namedControlScript.Replace("$controls$", controlScript.ToString());

            return namedControlScript;
        }*/


        protected virtual string GetControlScript()
        {
            return base.GetTemplateScriptResource(ControlTemplateName, GetType().BaseType);
        }


        protected virtual string GetCreateScript()
        {
            return base.GetTemplateScriptResource(CreateTemplateName, GetType().BaseType);
        }


        protected virtual string GetDynamicCreateScript()
        {
            return base.GetTemplateScriptResource(DynamicCreateTemplateName, GetType().BaseType);
        }


        protected virtual string GetNamedMethodScript()
        {
            return base.GetTemplateScriptResource(NamedMethodTemplateName, GetType().BaseType);
        }


        protected virtual string GetNamedPropertyScript()
        {
            return base.GetTemplateScriptResource(NamedPropertyTemplateName, GetType().BaseType);
        }


        protected virtual string GetNamedControlScript()
        {
            return base.GetTemplateScriptResource(NamedControlTemplateName, GetType().BaseType);
        }
		//------\\ Methods //-----------------------------------------------//
	}
}
